﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;

public partial class MasterPages_ConsumerMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
        }
    }

    protected void InitData()
    {
        //searchButton.Attributes.Add("onClick", "alert('Ouch, you clicked me!');");

        dropdownlist_restaurantdistrict.DataSource = BLL.Command.GetDistricts(BLL.Command.DistrictType.ForConsumer);
        dropdownlist_restaurantdistrict.DataBind();

        dropdownList_serveddistrict.DataSource = BLL.Command.GetDistricts(BLL.Command.DistrictType.ForConsumer);
        dropdownList_serveddistrict.DataBind();

        if (Session["RestaurantDistrictSelected"] != null)
        {
            dropdownlist_restaurantdistrict.SelectedValue = Session["RestaurantDistrictSelected"].ToString();
        }

        if (Session["ServedDistrictSelected"] != null)
        {
            dropdownList_serveddistrict.SelectedValue = Session["ServedDistrictSelected"].ToString();
        }

        //search_textbox.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + search_button.UniqueID + "','')");
    }

    public void DisableFilter()
    {
        dropdownlist_restaurantdistrict.Enabled = false;
        dropdownList_serveddistrict.Enabled = false;
    }
    //protected void OnClickSearchBtn(object sender, EventArgs e)
    //{
    //    string keywords = search_textbox.Text.Trim();
    //    Response.Redirect("~/Public/searchresults-keywords="+keywords+".aspx");
    //}
    protected void OnFilterSelectedIndexChanged(object sender, EventArgs e)
    {
        Int16 districtId = Convert.ToInt16(dropdownlist_restaurantdistrict.Items[dropdownlist_restaurantdistrict.SelectedIndex].Value.ToString());
        Int16 servedDistrictId = Convert.ToInt16(dropdownList_serveddistrict.Items[dropdownList_serveddistrict.SelectedIndex].Value.ToString());

        Session["RestaurantDistrictSelected"] = districtId;
        Session["ServedDistrictSelected"] = servedDistrictId;


        CallAllCPHEventHandlers("FilterSelectedIndexChanged", sender, districtId, servedDistrictId);
    }

    protected void CallAllCPHEventHandlers(String inMethodName, object inSender, Int16 districtId, Int16 servedDistrictId)
    {
        for (int i = 0; i < ContentPlaceHolders.Count; i += 1)
        {
            String cphName = (String)ContentPlaceHolders[i];
            ContentPlaceHolder me = (ContentPlaceHolder)Master.FindControl("Base_ContentPlaceHolder");
            ContentPlaceHolder cph = (ContentPlaceHolder)me.FindControl(cphName);
            Page page = cph.Page;
            Type pageType = page.GetType();
            MethodInfo cphMethod = pageType.GetMethod(inMethodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
            if (cphMethod != null)
            {
                object[] parameters = new object[3];
                parameters[0] = inSender;
                parameters[1] = (object)districtId;
                parameters[2] = (object)servedDistrictId;
                cphMethod.Invoke(page, parameters);
            }
        }
    }
}
